<template>
	<view class="content">
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item"
				:class="{ current: tabCurrentIndex === index }" @click="tabClick(index)">{{ item.text }}</view>
		</view>
		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData(tabIndex)">
					<!-- 空白页 -->
					<view v-if="tabItem.loaded === true && tabItem.orderList.length === 0">
						<u-empty text="暂无记录" mode="list">
						</u-empty>
					</view>
					<!-- 订单列表 -->
					<view v-else class="seller-view" :key="oderIndex" v-for="(order, oderIndex) in tabItem.orderList">
						<!-- 店铺名称 -->
						<view class="seller-info u-flex u-row-between">
						</view>
						<view>
							<view>
								<view class="goods-item-view">
									<view class="goods-info">
										<view class="goods-price">
											<span class='hb-money'>
												{{ order.applyPrice | unitPrice }}
											</span>
										</view>
									</view>
									<view class="zhekou">
										<span style="text-align: center;"
											v-if="order.fee">折扣率：{{ order.fee| formatWalletTwo }}</span>
										<image src="/static/jiantou.png" class="zk-image" mode="heightFix" />
									</view>
									<view class="goods-num">
										<image src="/static/renminbi.png" class="hb-image" mode="heightFix" />
										<span class='hb-money' style="color: #c34463;text-align: center;">
											{{ order.applyMoney | unitPrice }}
										</span>
									</view>
								</view>
							</view>
							<view class="btn-view u-flex u-row-between">
								<view class="description">
									<div>提现时间:</div>
									<div class="price">
										<span v-if="order.createTime" style="color: #909399;margin-left: 20rpx;">
											{{ order.createTime }}
										</span>
									</div>
								</view>
							</view>
							<view class="btn-view u-flex u-row-between">
								<view class="description" v-if="order.applyStatus == 'APPLY'">审核状态：申请中</view>
								<view class="description" v-if="order.applyStatus == 'CANCEL'">审核状态：撤销申请</view>
								<view class="description" v-if="order.applyStatus == 'VIA_AUDITING'">审核状态：审核通过</view>
								<view class="description" v-if="order.applyStatus == 'FAIL_AUDITING'">审核状态：审核未通过</view>
								<view v-if="order.applyStatus == 'APPLY'" class="descriptionsss">
									<span style="border: 1px solid #909399;padding: 10rpx 20rpx;border-radius: 24rpx;"
										@click="revoke(order)">撤销</span>
								</view>
							</view>
						</view>
					</view>
					<uni-load-more :status="tabItem.loadStatus"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
		<u-popup class="cancel-popup" v-model="cancelShow" mode="bottom" length="60%">
			<view class="header">取消订单</view>
			<view class="body">
				<view class="title">取消订单后，本单享有的优惠可能会一并取消，是否继续？</view>
				<view>
					<u-radio-group v-model="reason">
						<view class="value">
							<view class="radio-view" :key="index" v-for="(item, index) in cancelList">
								<u-radio :active-color="lightColor" label-size="25" shape="circle" :name="item.reason"
									@change="reasonChange">{{ item.reason }}</u-radio>
							</view>
						</view>
					</u-radio-group>
				</view>
			</view>
			<view class="footer">
				<u-button size="medium" ripple v-if="reason" shape="circle" @click="submitCancel">提交</u-button>
			</view>
		</u-popup>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	const StoreTab = [{
			state: 0,
			text: "推广钱包",
			owner: 'PROMOTE',
			loadStatus: "more",
			orderList: [],
			pageNumber: 1,
			pageSize: 10
		},
		{
			state: 1,
			text: "销售钱包",
			owner: 'SALE',
			loadStatus: "more",
			orderList: [],
			pageNumber: 1,
			pageSize: 10
		},
	]
	const CityTab = [{
		state: 1,
		text: "推广钱包-服务费",
		owner: 'PROMOTE_FW',
		loadStatus: "more",
		orderList: [],
		pageNumber: 1,
		pageSize: 10
	}, {
		state: 0,
		text: "推广钱包-会员费",
		owner: 'PROMOTE_HY',
		loadStatus: "more",
		orderList: [],
		pageNumber: 1,
		pageSize: 10
	}]
	const TsSyTab = [{
		state: 1,
		text: "推广钱包",
		owner: 'PROMOTE',
		loadStatus: "more",
		orderList: [],
		pageNumber: 1,
		pageSize: 10
	}]
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue";
	import {
		withdrawlist,
		withdrawgetsums,
		applycheixao
	} from "@/api/withdrawal.js";
	import {
		getClearReason
	} from "@/api/after-sale.js";
	import storage from "@/utils/storage.js";
	import LiLiWXPay from "@/js_sdk/lili-pay/wx-pay.js";
	import payPassword from "@/components/pay-password/pay-password.vue"
	export default {
		components: {
			uniLoadMore,
			payPassword
		},
		data() {
			return {
				lightColor: this.$lightColor,
				tabCurrentIndex: -1, //导航栏索引
				navList: [],
				status: "", //接收导航栏状态
				params: {
					pageNumber: 1,
					pageSize: 10
				},
				orderStatus: [
					//订单状态
					{
						orderStatus: "", //全部
					},
					{
						orderStatus: "SALE", //代付款
					},
				],
				cancelShow: false, //是否显示取消
				orderSn: "", //ordersn
				reason: "", //取消原因
				cancelList: "", //取消列表
			};
		},
		/**
		 * 跳转到个人中心
		 */
		onBackPress(e) {
			if (e.from == "backbutton") {
				uni.reLaunch({
					url: "/pages/tabbar/user/my",
				});
				return true; //阻止默认返回行为
			}
		},
		onPullDownRefresh() {
			this.loadData(this.status);
		},
		onLoad(options) {
			let userInfo = storage.getUserInfo();
			if (userInfo && userInfo.haveStore) {
				this.navList = StoreTab
			} else if (userInfo.partnerType == 4) {
				this.navList = CityTab
			} else {
				this.navList = TsSyTab
			}
			if (options.owner) {
				if (options.owner == 'SALE' || options.owner === 'BUYER_PROMOTE_HY') {
					this.status = 1;
					this.tabCurrentIndex = this.status;
				} else {
					this.status = 0;
					this.tabCurrentIndex = this.status;
				}
			} else {
				this.status = 0;
				this.tabCurrentIndex = this.status;
			}
			this.initData()
		},
		methods: {
			/**
			 * 初始化数据
			 */
			initData() {
				let index = this.tabCurrentIndex
				//切换标签页将所有的页数都重置为1
				this.navList.forEach((res) => {
					res.pageNumber = 1;
					res.loadStatus = "more";
					res.orderList = [];
				});
				this.navList[index].pageNumber = 1;
				this.navList[index].loadStatus = "more";
				this.navList[index].orderList = [];
				this.loadData(this.tabCurrentIndex);
			},
			/**
			 * 获取销售提现列表
			 */
			loadData(index) {
				if (this.navList[index].loadStatus == "noMore") return
				withdrawlist({
					owner: this.navList[index].owner,
					pageNumber: this.navList[index].pageNumber,
					pageSize: this.navList[index].pageSize
				}).then((res) => {
					uni.stopPullDownRefresh();
					if (!res.data.success) {
						this.navList[index].loadStatus = "noMore";
						return false;
					}
					let orderList = res.data.result.records;
					if (orderList.length == 0) {
						this.navList[index].loadStatus = "noMore";
					} else if (orderList.length < 10) {
						this.navList[index].loadStatus = "noMore";
					}
					if (orderList.length > 0) {
						this.navList[index].orderList =
							this.navList[index].orderList.concat(orderList);
						this.navList[index].pageNumber += 1;
					}
				});
			},
			// 撤销
			revoke(row) {
				if (this.navList[this.tabCurrentIndex].owner == 'SALE') {
					let parse = {
						idArr: row.id,
					}
					uni.showModal({
						title: '撤销提示',
						content: '撤销当前提现申请后，金额将退回销售钱包',
						confirmText: '确定',
						cancelText: '取消',
						success: data => {
							if (data.confirm) {
								withdrawgetsums(parse).then((res) => {
									if (res.data.code == 84001) {
										uni.showToast({
											title: "撤销成功",
											icon: "none",
										});
										setTimeout(() => {
											this.initData()
										}, 1000)
									} else {
										uni.showToast({
											title: "撤销失败",
											icon: "none",
										});
									}
								});
							}
						}
					});
				} else {
					let parse = {
						applyId: row.id,
					}
					uni.showModal({
						title: '撤销提示',
						content: '撤销当前提现申请后，金额将退回推广钱包',
						confirmText: '确定',
						cancelText: '取消',
						success: data => {
							if (data.confirm) {
								applycheixao(parse).then((res) => {
									console.log(res)
									if (res.data && res.data == true) {
										uni.showToast({
											title: "撤销成功",
											icon: "none",
										});
										setTimeout(() => {
											this.initData()
										}, 1000)
									} else {
										uni.showToast({
											title: "撤销失败",
											icon: "none",
										});
									}
								});
							}
						}
					});
				}

			},
			//swiper 切换监听
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.initData()
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
		},
	};
</script>

<style lang="scss" scoped>
	page,
	.content {
		background: $page-color-base;
		height: 100%;
	}

	.swiper-box {
		height: calc(100vh - 40px);
		// #ifdef H5
		height: calc(100vh - 40px - 44px);
		// #endif
	}

	.list-scroll-content {
		height: 100%;
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 26rpx;
			color: $font-color-light;
			position: relative;

			&.current {
				color: $main-color;

				&:after {
					content: "";
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $main-color;
				}
			}
		}
	}

	.uni-swiper-item {
		height: auto;
	}

	.seller-view {
		border-radius: 20rpx;
		background-color: #fff;
		margin: 20rpx 0rpx;

		.seller-info {
			height: 70rpx;
			padding: 0 20rpx;

			.seller-name {
				font-size: 28rpx;
				font-weight: 600;
				display: flex;
				flex-direction: row;

				.name {
					margin-left: 15rpx;
					margin-top: -2rpx;
				}
			}

			.order-sn {
				color: $aider-light-color;
				font-size: 26rpx;
			}
		}

		.goods-item-view {
			display: flex;
			flex-wrap: wrap;
			flex-direction: row;
			justify-content: center;
			justify-content: space-between;
			padding: 10rpx 20rpx;

			.goods-img {
				width: 131rpx;
				height: 131rpx;
				margin-right: 10rpx;
				margin-bottom: 10rpx;
			}

			.goods-info {
				padding-left: 30rpx;
				// flex: 1;

				.goods-title {
					margin-bottom: 10rpx;
					color: #333333;
				}

				.goods-specs {
					font-size: 24rpx;
					margin-bottom: 10rpx;
					color: #cccccc;
				}

				.goods-price {
					font-size: 28rpx;
					margin-bottom: 10rpx;
					color: $aider-light-color;
					display: flex;
					flex-direction: column;
					justify-content: center;
				}
			}

			.goods-num {
				// width: 60rpx;
				// color: #c34463;
				// color: $main-color;
				padding-right: 30rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
			}
		}

		.btn-view {
			padding: 25rpx 30rpx;
			font-size: 26rpx;

			.description {
				display: flex;
				color: #909399;
				size: 24rpx;
				flex: 1;

				.price {
					color: $main-color;
				}
			}

			.descriptionsss {
				display: flex;
				color: #909399;
				size: 24rpx;
				margin-right: 30rpx;

				.price {
					color: $main-color;
				}
			}
		}
	}

	.cancel-popup {
		.header {
			display: flex;
			flex-direction: row;
			justify-content: center;
			margin: 15rpx 0rpx;
		}

		.body {
			padding: 30rpx;

			.title {
				font-weight: 600;
			}

			.value {
				display: flex;
				flex-direction: column;
				margin: 20rpx 0;

				.radio-view {
					margin: 20rpx 0rpx;
				}
			}
		}

		.footer {
			text-align: center;
		}
	}

	.cancel-btn {
		color: #999999 !important;
		border-color: #999999 !important;
		margin-left: 15rpx;
		height: 60rpx;
	}

	.pay-btn {
		// #ifndef MP-WEIXIN
		background-color: $light-color !important;
		// #endif
		color: #ffffff !important;
		margin-left: 15rpx;
		height: 60rpx;
	}

	.rebuy-btn {
		color: $light-color !important;
		border-color: $light-color !important;
		background-color: #ffffff !important;
		margin-left: 15rpx;
		height: 60rpx;
	}

	.hb-image {
		margin-bottom: 10upx;
	}

	.zhekou {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}
</style>
<style>
	.zk-image {
		height: 36rpx;
	}
</style>
